package com.yousu.dashijixm.exception;

import com.yousu.dashijixm.pojo.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * @Author: YouSu
 * @Version: 1.0
 * @CreateTime: 2023-11-18  17:47
 */
//全局异常处理器
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        e.printStackTrace();
        //使用了springboot中的StringUtils工具类
        return Result.error(StringUtils.hasLength(e.getMessage())? e.getMessage() : "操作失败，请稍后尝试");
    }
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public Result exception(MaxUploadSizeExceededException e) {
        System.out.println(e.getMessage());
        return Result.error("上传文件大小超过限制"+e.getMessage());
    }
}
